home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_quik.lha / quickdraw / src / poly_calls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-09  |  7.7 KB  |  397 lines

  1. /*------------------------------- poly_calls.c -------------------------------*/
  2. /* Copyright 1989 Brown University -- Jeffrey Vogel                           */
  3. /*----------------------------------------------------------------------------*/
  4.  
  5.  
  6. /*--------------------------------- Includes ---------------------------------*/
  7. /*----------------------------------------------------------------------------*/
  8.  
  9. #include "qd_local.h"
  10.  
  11.  
  12. /*-------------------------------- SetPoints ---------------------------------*/
  13. /*----------------------------------------------------------------------------*/
  14.  
  15. static
  16. void
  17. SetPoints(poly, d)
  18. Polygon   *poly;
  19. XPoint    d[];
  20. {
  21.    register int i;
  22.  
  23.    for (i = 0; i < poly->num_vertices; i++) {
  24.       d[i].x = (short) poly->vertices[i].x;
  25.       d[i].y = (short) poly->vertices[i].y;
  26.    }
  27.    d[poly->num_vertices].x = poly->vertices[0].x;
  28.    d[poly->num_vertices].y = poly->vertices[0].y;
  29. }
  30.  
  31. /*-------------------------------- CreatePoly --------------------------------*/
  32. /*----------------------------------------------------------------------------*/
  33.  
  34. Polygon
  35. CreatePoly()
  36. {
  37.    Polygon a;
  38.  
  39.    /*** error check ***/
  40.    if (!QDrunning) {
  41.       QDerror("ERROR CreatePoly: QuickDraw not initialized.");
  42.       return;
  43.    }
  44.  
  45.    a.num_vertices = 0;
  46.    a.vertices = NULL;
  47.   
  48.    return a;
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. /*--------------------------------- CopyPoly ---------------------------------*/
  68. /*----------------------------------------------------------------------------*/
  69.  
  70. Polygon
  71. CopyPoly(s)
  72. Polygon s;
  73. {
  74.    Polygon d;
  75.    int     a;
  76.  
  77.    /*** error check ***/
  78.    if (!QDrunning) {
  79.       QDerror("ERROR CopyPoly: QuickDraw not initialized.");
  80.       return;
  81.    }
  82.  
  83.    a = d.num_vertices = s.num_vertices;
  84.    d.vertices = (Point *) malloc(a * sizeof(Point));
  85.    bcopy((char *) s.vertices, (char *) d.vertices,
  86.          a * sizeof(Point));
  87. }
  88.  
  89. /*-------------------------------- PolyAddPt ---------------------------------*/
  90. /*----------------------------------------------------------------------------*/
  91.  
  92. void
  93. PolyAddPt(poly, x, y)
  94. Polygon *poly;
  95. int     x, y;
  96. {
  97.    /*** error check ***/
  98.    if (!QDrunning) {
  99.       QDerror("ERROR PolyAddPt: QuickDraw not initialized.");
  100.       return;
  101.    }
  102.  
  103.    /*** set number, malloc, and set point ***/
  104.    poly->num_vertices++;
  105.    if (poly->vertices)
  106.       poly->vertices = (Point *) realloc((char *) poly->vertices,
  107.                                          poly->num_vertices * sizeof(Point));
  108.    else
  109.       poly->vertices = (Point *) malloc(poly->num_vertices * sizeof(Point));
  110.    poly->vertices[poly->num_vertices-1].x = x;
  111.    poly->vertices[poly->num_vertices-1].y = y;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*------------------------------- PolyQueryPt --------------------------------*/
  134. /*----------------------------------------------------------------------------*/
  135.  
  136. void
  137. PolyQueryPt(poly, index, x, y)
  138. Polygon poly;
  139. int     index;
  140. int     *x, *y;
  141. {
  142.    /*** error check ***/
  143.    if (!QDrunning) {
  144.       QDerror("ERROR PolyQueryPt: QuickDraw not initialized.");
  145.       return;
  146.    }
  147.    if ((index > poly.num_vertices) || (index < 1)) {
  148.       QDerror("ERROR PolyQueryPt: invalid index.");
  149.       return;
  150.    }
  151.  
  152.    *x = poly.vertices[index-1].x;
  153.    *y = poly.vertices[index-1].y;
  154. }
  155.  
  156.  
  157. /*-------------------------------- FramePoly ---------------------------------*/
  158. /*----------------------------------------------------------------------------*/
  159.  
  160. void
  161. FramePoly(poly)
  162. Polygon  poly;
  163. {
  164.    XPoint   *xpoints;
  165.  
  166.    /*** error check ***/
  167.    if (!QDrunning) {
  168.       QDerror("ERROR FramePoly: QuickDraw not initialized.");
  169.       return;
  170.    }
  171.  
  172.    /*** trivial reject ***/
  173.    if (poly.num_vertices < 3) {
  174.       QDerror("ERROR FramePoly: polygon needs at least 3 vertices.");
  175.       return;
  176.    }
  177.  
  178.    /*** set up points, draw them, and free them ***/
  179.    xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
  180.    SetPoints(&poly, xpoints);
  181.    XDrawLines(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
  182.               CoordModeOrigin);
  183.    free(xpoints);
  184.  
  185.    /*** flush the request ***/
  186.    XFlush(QDdisplay);
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. /*-------------------------------- PaintPoly ---------------------------------*/
  200. /*----------------------------------------------------------------------------*/
  201.  
  202. void
  203. PaintPoly(poly)
  204. Polygon  poly;
  205. {
  206.    XPoint  *xpoints;
  207.  
  208.    /*** error check ***/
  209.    if (!QDrunning) {
  210.       QDerror("ERROR PaintPoly: QuickDraw not initialized.");
  211.       return;
  212.    }
  213.  
  214.    /*** trivial reject ***/
  215.    if (poly.num_vertices < 3) {
  216.       QDerror("ERROR PaintPoly: polygon needs at least 3 vertices.");
  217.       return;
  218.    }
  219.  
  220.    /*** set up points, draw them, and free them ***/
  221.    xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
  222.    SetPoints(&poly, xpoints);
  223.    XFillPolygon(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
  224.                 Complex, CoordModeOrigin);
  225.    free(xpoints);
  226.  
  227.    /*** flush the request ***/
  228.    XFlush(QDdisplay);
  229. }
  230.  
  231.  
  232. /*-------------------------------- ErasePoly ---------------------------------*/
  233. /*----------------------------------------------------------------------------*/
  234.  
  235. void
  236. ErasePoly(poly)
  237. Polygon  poly;
  238. {
  239.    XPoint  *xpoints;
  240.  
  241.    /*** error check ***/
  242.    if (!QDrunning) {
  243.       QDerror("ERROR ErasePoly: QuickDraw not initialized.");
  244.       return;
  245.    }
  246.  
  247.    /*** trivial reject ***/
  248.    if (poly.num_vertices < 3) {
  249.       QDerror("ERROR ErasePoly: polygon needs at least 3 vertices.");
  250.       return;
  251.    }
  252.  
  253.    /*** set up points, draw them, and free them ***/
  254.    xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
  255.    SetPoints(&poly, xpoints);
  256.    XSetFunction(QDdisplay, QDgc, GXclear);
  257.    XFillPolygon(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
  258.                 Complex, CoordModeOrigin);
  259.    XChangeGC(QDdisplay, QDgc, GCFunction, &QDgcValues);
  260.    free(xpoints);
  261.  
  262.  
  263.  
  264.  
  265.    /*** flush the request ***/
  266.    XFlush(QDdisplay);
  267. }
  268.  
  269.  
  270.  
  271.  
  272. /*-------------------------------- OffsetPoly --------------------------------*/
  273. /*----------------------------------------------------------------------------*/
  274.  
  275. void
  276. OffsetPoly(poly, dx, dy)
  277. Polygon  *poly;
  278. int      dx, dy;
  279. {
  280.    register int i;
  281.  
  282.    /*** error check ***/
  283.    if (!QDrunning) {
  284.       QDerror("ERROR OffsetPoly: QuickDraw not initialized.");
  285.       return;
  286.    }
  287.  
  288.    /*** trivial reject ***/
  289.    if (poly->num_vertices < 3) {
  290.       QDerror("ERROR OffsetPoly: polygon needs at least 3 vertices.");
  291.       return;
  292.    }
  293.  
  294.    for (i = 0; i < poly->num_vertices; i++) {
  295.       poly->vertices[i].x += dx;
  296.       poly->vertices[i].y += dy;
  297.    }
  298. }
  299.  
  300.  
  301. /*-------------------------------- InsetPoly ---------------------------------*/
  302. /*----------------------------------------------------------------------------*/
  303.  
  304. void
  305. InsetPoly(poly, dx, dy)
  306. Polygon  *poly;
  307. int      dx, dy;
  308. {
  309.    register int i;
  310.    int       t, b, l, r, cx, cy;
  311.  
  312.    /*** error check ***/
  313.    if (!QDrunning) {
  314.       QDerror("ERROR InsetPoly: QuickDraw not initialized.");
  315.       return;
  316.    }
  317.  
  318.    /*** trivial reject ***/
  319.    if (poly->num_vertices < 3) {
  320.       QDerror("ERROR InsetPoly: polygon needs at least 3 vertices.");
  321.       return;
  322.    }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.    /*** get boundaries ***/
  332.    t = b = poly->vertices[0].y;
  333.    r = l = poly->vertices[0].x;
  334.    for (i = 0; i < poly->num_vertices; i++) {
  335.       if (poly->vertices[i].x > r)
  336.          r = poly->vertices[i].x;
  337.       if (poly->vertices[i].x < l)
  338.          l = poly->vertices[i].x;
  339.       if (poly->vertices[i].y > b)
  340.          b = poly->vertices[i].y;
  341.       if (poly->vertices[i].x < t)
  342.          t = poly->vertices[i].y;
  343.    }
  344.  
  345.    /*** get center ***/
  346.    cx = (r + l) / 2;
  347.    cy = (t + b) / 2;
  348.  
  349.    /*** inset each towards center ***/
  350.    for (i = 0; i < poly->num_vertices; i++) {
  351.       if (poly->vertices[i].x < cx)
  352.          poly->vertices[i].x += dx;
  353.       else
  354.          poly->vertices[i].x -= dx;
  355.       if (poly->vertices[i].y < cy)
  356.          poly->vertices[i].y += dy;
  357.       else
  358.          poly->vertices[i].y -= dy;
  359.    }
  360. }
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.